package com.yunhui.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunhui.entity.Order;
import com.yunhui.entity.PeopleInfo;
import com.yunhui.entity.RouteJb;
import com.yunhui.entity.SubscribePeople;
import com.yunhui.mapper.OrderMapper;
import com.yunhui.service.OrderService;
import com.yunhui.service.RouteIssuedService;
import com.yunhui.service.RouteJbService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2019-12-09
 */
@Service

public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    /**
     * 查询所有订单信息
     * @return
     */
    @Override
    public List<Order> findAllOrder() {
        return orderMapper.findAllOrder();
    }

    /**
     * 根据用户Id查询
     * @return
     */
    @Override
    public List<Order> findOrderById(String O_date,Integer sid,Integer uid,int currentPageNo,int pageSize) {
        //需要判断状态
        return orderMapper.findOrderById(O_date,sid,uid,currentPageNo,pageSize);
    }

    /**
     * 商家---根据商家id 查询订单信息
     * @param O_date
     * @param sjId  商家登录时的id
     * @param currentPageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<Order> findOrderBysjId(String O_date,String title,Integer sjId, int currentPageNo, int pageSize) {
        return orderMapper.findOrderBysjId(O_date,title,sjId,currentPageNo,pageSize);
    }


    /**
     *
     * 商家---查詢今日訂單
     * @param
     * @return
     */
    @Override
    public List<Order> findTodayOrder(Integer id){
        return orderMapper.findTodayOrder(id);
    }


    /**
     * 商家---加载行程信息
     * @param sjId
     * @return
     */
    @Override
    public List<RouteJb> findTitle(Integer sjId) {
        return orderMapper.findTitle(sjId);
    }

    /**
     *  商家---加载行程编号
     * @return
     */
    @Override
    public List<Order> findOrderSid() {
        return orderMapper.findOrderSid();
    }

    /**
     *
     *   订单页面：加载信息
     */
    @Override
    public List<Order> findOrder(Integer id) {
        return orderMapper.findOrder(id);
    }

    /**
     * 订单页面：提交添加联系人
     * @return
     */
    @Override
    public int addSubscribePeople(SubscribePeople subscribePeople) {
        int num = orderMapper.addSubscribePeople(subscribePeople);
        if(num>0){
            System.out.println("SubscribePeople Success");
        }else {
            System.out.println("SubscribePeople Failed");
        }
        return num;
    }


    /**
     * 订单页面：添加游客信息
     * @param peopleInfo
     * @return
     */
    @Override
    public int addPeopleInfo(PeopleInfo peopleInfo) {
        int num = orderMapper.addPeopleInfo(peopleInfo);
        if(num>0){
            System.out.println("PeopleInfo Success");
        }else {
            System.out.println("PeopleInfo Failed");
        }
        return num;
    }


    /**
     * 订单页面：根据行程编号查询显示信息
     * @param id
     * @return
     */
    @Override
    public List<RouteJb> findRouteInfo(Integer id) {
        return orderMapper.findRouteInfo(id);
    }


    /**
     * 后台：详细信息显示
     * 根据行程发布Id获得基本Id
     * @param
     * @return
     */
    @Override
    public Integer findRoute_issued(Integer sid) {
        return orderMapper.findRoute_issued(sid);
    }


    /**
     * 查询联系人信息
     * @param oid
     * @return
     */
    @Override
    public List<SubscribePeople> findSubscribePeople(Integer oid) {
        return orderMapper.findSubscribePeople(oid);
    }

    /**
     * 查询游客信息
     * @param oid
     * @return
     */
    @Override
    public List<PeopleInfo> findPeopleInfo(Integer oid) {
        return orderMapper.findPeopleInfo(oid);
    }


    /**
     * 我的订单页面：查询信息
     * @param index
     * @param size
     * @param status
     * @param uid
     * @return
     */
    @Override
    public List<Order> ListByPage(Integer index, Integer size, String status, Integer uid) {

        return orderMapper.ListByPage((index-1)*size,size,status,uid);
    }

    @Override
    public int CountByPage(String status, Integer uid) {
        return orderMapper.CountByPage(status,uid);
    }


    /**
     * 订单详细信息页面
     *
     */

    //修改联系人信息   根据订单编号修改
    @Override
    public int updateSubscribePeople(SubscribePeople subscribePeople) {
        int cout = orderMapper.updateSubscribePeople(subscribePeople);
        if(cout>0){
            System.out.println("修改联系人成功！");
        }else {
            System.out.println("修改联系人失败！");
        }
        return cout;
    }
    //修改游客信息	根据游客姓名修改
    @Override
    public int updatePeopleInfo(PeopleInfo peopleInfo) {
        int cout = orderMapper.updatePeopleInfo(peopleInfo);
        if(cout>0){
            System.out.println("修改游客成功！");
        }else {
            System.out.println("修改游客失败！");
        }
        return cout;
    }

    //#根据订单编号查询信息
    @Override
    public List<Order> findInfo(Integer id) {
        return orderMapper.findInfo(id);
    }


    /**
     * 添加订单，订单联系人，订单的出行人的信息
     * @param order
     * @param subscribePeople
     * @param list
     * @return
     */
    //行程发布的业务层
    @Resource
    private  RouteIssuedService routeIssuedService;


    //行程基本信息业务层
    @Resource
    private RouteJbService routeJbService;


    //消息队列的对象
    @Resource
    private RabbitTemplate template;
    @Override
    public Boolean addOrder(Order order, SubscribePeople subscribePeople, List<PeopleInfo> list) {
       /* boolean num = false ;

        //用户人数
        int userNum = list.size();

        //获取行程基础总人数
        int jiNum =routeJbService.getById(routeIssuedService.getById(order.getSid()).getJbId()).getMaxPerson();

        int countFaCount = routeIssuedService.selectByJbId(order.getSid());

        if((userNum+countFaCount)>=jiNum){
            return  false;
        }

        try{

            //首先添加订单
            orderMapper.insert(order);
            subscribePeople.setO_id(order.getId());
            //添加订单联系人
            orderMapper.addSubscribePeople(subscribePeople);

            //循环添加订单出行人信息
            for(PeopleInfo p:list){
                //添加订单编号
                p.setO_id(order.getId());
                orderMapper.addPeopleInfo(p);


            }
            num = true;
        }catch (Exception e){
            e.printStackTrace();
            num  = false;
        }
        return num;*/
        if(order==null||subscribePeople==null||list==null) {
            return false;
        }
        Object [] objects = {order,subscribePeople,list};
        //
        template.convertAndSend("topicExchange",  "topic.queueorder",objects);
        return true;

    }



}
